//
//  MHFeedbackViewController.m
//  MHO
//
//  Created by Hien NV on 6/18/14.
//  Copyright (c) 2014 maihoang. All rights reserved.
//

#import "MHFeedbackViewController.h"
#import "Lib.h"

#define kTextViewPlaceHolder    @"Xin nhập góp ý ..."

@interface MHFeedbackViewController ()

@end

@implementation MHFeedbackViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navibarView.leftButton setImage:[UIImage imageNamed:kFileBtnBack] forState:0];
    [self.navibarView.searchButton setHidden:YES];
    [self.navibarView.rightButton setImage:nil forState:0];
    [self.navibarView.rightButton setTitle:@"Gửi" forState:0];
    [self.navibarView.rightButton setBackgroundColor:[UIColor clearColor]];
    [self.navibarView.rightButton setTitleColor:[UIColor whiteColor] forState:0];
    self.navibarView.rightButton.layer.borderColor = [UIColor whiteColor].CGColor;
    self.navibarView.rightButton.layer.borderWidth = 1;
    self.navibarView.rightButton.layer.cornerRadius = 4;
    CGRect frame = self.navibarView.rightButton.frame;
    frame.origin.x -= 20;
    frame.origin.y -= 5;
    frame.size.height += 10;
    frame.size.width += 20;
    self.navibarView.rightButton.frame = frame;
    [self.navibarView setRootViewController:nil];
    
    [self.commentTextView setText:kTextViewPlaceHolder];
    self.commentTextView.textColor = [UIColor lightGrayColor];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)rightButtonPressed:(id)button {
    if ([Lib isUserLogin]) {
        NSString* text = self.commentTextView.text;
        if (text && text.length > 0 && ![text isEqualToString:kTextViewPlaceHolder]) {
            [Lib showLoadingView];
            
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSString* url = [NSString stringWithFormat:@"http://mobile.mho.vn/services/gopy?user=%@&content=%@&key=%@", [Lib stringForKey:@"Email"], text, kServerKey];
                NSString* retVal = [ServiceLib sendGetRequest:url];
                if (retVal) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [Lib removeLoadingView];
                        [UIAlertView showWithTitle:@"Thông báo" message:@"Gửi góp ý thành công! Xin cảm ơn bạn!" cancelButtonTitle:@"OK" otherButtonTitles:nil tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            
                        }];
                    });
                }
            });
        }
    }
    else {
        [UIAlertView showWithTitle:@"Thông báo" message:@"Bạn cần đăng nhập để gửi góp ý cho chúng tôi! Xin cảm ơn!" cancelButtonTitle:@"OK" otherButtonTitles:nil tapBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
            
        }];
    }
}


#pragma mark - UITextViewDelegate
- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([textView.text isEqualToString:kTextViewPlaceHolder] && textView.textColor == [UIColor lightGrayColor]) {
        textView.textColor = [UIColor blackColor];
        textView.text = @"";
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length == 0) {
        textView.text = kTextViewPlaceHolder;
        textView.textColor = [UIColor lightGrayColor];
    }
}

@end
